package com.deyuanyun.pic.ae.controller;

import com.deyuanyun.pic.ae.controller.dto.AEBaseAudit;
import com.deyuanyun.pic.ae.controller.dto.GeologicHazardEdit;
import com.deyuanyun.pic.ae.controller.dto.GeologicHazardQuery;
import com.deyuanyun.pic.ae.controller.dto.SewerEdit;
import com.deyuanyun.pic.ae.controller.vo.GeologicHazardAuditVO;
import com.deyuanyun.pic.ae.controller.vo.GeologicHazardVO;
import com.deyuanyun.pic.ae.controller.vo.SewerAuditVO;
import com.deyuanyun.pic.ae.domain.GeologicHazard;
import com.deyuanyun.pic.ae.domain.GeologicHazard;
import com.deyuanyun.pic.ae.enumeration.AEExportEnum;
import com.deyuanyun.pic.ae.enumeration.FindTypeEnum;
import com.deyuanyun.pic.ae.service.GeologicHazardService;
import com.deyuanyun.pic.common.dto.PageJsonBean;
import com.deyuanyun.pic.common.util.StringUtils;
import com.deyuanyun.pic.common.util.ajax.AjaxSupport;
import com.deyuanyun.pic.common.util.poi.ExcelUtil;
import com.deyuanyun.pic.common.util.web.Asserts;
import com.deyuanyun.pic.settings.util.DownExcelUtil;
import com.github.pagehelper.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * “沿线地质灾害” 管理控制类
 *
 * 方法详情：<br/>
 *
 * @author huanglw
 * @date  2016-11-9
 *
 * History: [修改人][时间][修改描述]
 */
@Controller
@RequestMapping("/ae/geologicHazard")
public class GeologicHazardController extends AEBaseController {

    @Resource
    private GeologicHazardService geologicHazardService;

    /**
     * 跳转到 “沿线地质灾害” 管理页面
     *
     * @return
     */
    @RequestMapping("/index.html")
    public ModelAndView index(){
        return forword("/ae/geologicHazard");
    }

    /**
     * 条件 查询“沿线地质灾害”
     *
     * @param geologicHazardQuery
     */
    @RequestMapping("/query.json")
    @ResponseBody
    public void query(GeologicHazardQuery geologicHazardQuery) {
        PageJsonBean jsonBean = new PageJsonBean();
        jsonBean.setList(this.queryGeologicHazard(geologicHazardQuery));
        AjaxSupport.sendSuccessText("success", jsonBean);
    }

    /**
     * 条件 导出“沿线地质灾害”
     *
     * @param geologicHazardQuery
     */
    @RequestMapping("/export.json")
    @ResponseBody
    public void export(GeologicHazardQuery geologicHazardQuery) throws IOException {
        List list = this.queryGeologicHazard(geologicHazardQuery);
        AEExportEnum exportEnum = AEExportEnum.GeologicHazard;
        String[] columnKeys = {"pipeLineName", "type", "riskProbabilityScore", "riskProbabilityLevel", "beginNagmInput",
                "beginNagmMileageInput", "endNagmInput", "endNagmMileageInput", "locationDescription", "consequenceLevel",
                "riskLevel", "recorder", "recordTime", "remarks"};
        DownExcelUtil.export(exportEnum.getFileName(), aeTemplatePath+exportEnum.getTempPath(), exportEnum.getBeginRow(), list, columnKeys);
    }

    /**
     * 根据id查询 “沿线地质灾害”
     *
     * @param id
     */
    @RequestMapping("/find.json")
    @ResponseBody
    public void find(String id, FindTypeEnum type) throws IOException {

        Asserts.assertLogic(StringUtils.isNullEmpty(id), "地下排水排污设施id 不能为空");
        GeologicHazard geologicHazard = geologicHazardService.find(id);
        if (FindTypeEnum.edit.equals(type)) {
            GeologicHazardEdit geologicHazardEdit = new GeologicHazardEdit();
            geologicHazardEdit.fromDomain(geologicHazard);
            AjaxSupport.sendSuccessText("", geologicHazardEdit);
        }
        if (FindTypeEnum.show.equals(type)) {
            GeologicHazardAuditVO geologicHazardAuditVO = new GeologicHazardAuditVO();
            geologicHazardAuditVO.fromDomain(geologicHazard);
            AjaxSupport.sendSuccessText("", geologicHazardAuditVO);
        }
        AjaxSupport.sendSuccessText("", "类型错误，没有任何数据");
    }

    /**
     * 创建 或者 编辑“沿线地质灾害”
     *
     * @param geologicHazardEdit
     */
    @RequestMapping("/edit.json")
    @ResponseBody
    public void edit(GeologicHazardEdit geologicHazardEdit) {
        int updateRows = geologicHazardService.edit(geologicHazardEdit.toDomain(), geologicHazardEdit.getImgs());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 审核“沿线地质灾害”
     *
     * @param aeBaseAudit
     */
    @RequestMapping("/audit.json")
    @ResponseBody
    public void audit(AEBaseAudit aeBaseAudit) {
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getId()), "地下排水排污设施id 不能为空");
        Asserts.assertLogic(StringUtils.isNullEmpty(aeBaseAudit.getDataStated()), "审核状态 不能为空");
        int updateRows = geologicHazardService.audit(aeBaseAudit.toGeologicHazard());
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    /**
     * 删除“沿线地质灾害”
     *
     * @param id
     */
    @RequestMapping("/delete.json")
    @ResponseBody
    public void delete(String id) {
        int updateRows = geologicHazardService.delete(id);
        AjaxSupport.sendSuccessText("success", updateRows);
    }

    private Page<GeologicHazardVO> queryGeologicHazard(GeologicHazardQuery geologicHazardQuery){
        return new GeologicHazardVO().fromDomainList(geologicHazardService.query(geologicHazardQuery));
    }
}